package com.ruoyi.yljf.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.yljf.domain.GoodsOrder;
import com.ruoyi.yljf.domain.User;
import com.ruoyi.yljf.domain.dto.BlessingDTO;
import com.ruoyi.yljf.domain.vo.BlessingVO;
import com.ruoyi.yljf.service.IUserService;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.yljf.mapper.BlessingMapper;
import com.ruoyi.yljf.domain.Blessing;
import com.ruoyi.yljf.service.IBlessingService;

import javax.annotation.Resource;

/**
 * 祈福订单Service业务层处理
 * 
 * @author metaee
 * @date 2025-03-26
 */
@Service
public class BlessingServiceImpl extends ServiceImpl<BlessingMapper, Blessing> implements IBlessingService {
    @Resource
    private BlessingMapper blessingMapper;

	@Resource
	private IUserService userService;
    
    /**
     * 查询祈福订单列表
     * 
     * @param blessing 祈福订单
     * @return 祈福订单
     */
    @Override
    public List<Blessing> selectBlessingList(Blessing blessing) {
    	LambdaQueryWrapper<Blessing> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotEmpty(blessing.getNumber())) {
			queryWrapper.eq(Blessing::getNumber, blessing.getNumber());
		}
        if (StringUtils.isNotNull(blessing.getUserId())) {
			queryWrapper.eq(Blessing::getUserId, blessing.getUserId());
		}
        if (StringUtils.isNotNull(blessing.getStatus())) {
			queryWrapper.eq(Blessing::getStatus, blessing.getStatus());
		}
        if (StringUtils.isNotNull(blessing.getDishId())) {
			queryWrapper.eq(Blessing::getDishId, blessing.getDishId());
		}
        if (StringUtils.isNotEmpty(blessing.getName())) {
			queryWrapper.like(Blessing::getName, blessing.getName());
		}
        if (StringUtils.isNotEmpty(blessing.getMessage())) {
			queryWrapper.eq(Blessing::getMessage, blessing.getMessage());
		}
        if (StringUtils.isNotNull(blessing.getAmount())) {
			queryWrapper.eq(Blessing::getAmount, blessing.getAmount());
		}
        if (StringUtils.isNotEmpty(blessing.getIsOpen())) {
			queryWrapper.eq(Blessing::getIsOpen, blessing.getIsOpen());
		}
        if (StringUtils.isNotNull(blessing.getCheckoutTime())) {
			queryWrapper.eq(Blessing::getCheckoutTime, blessing.getCheckoutTime());
		}
        if (StringUtils.isNotNull(blessing.getFinishTime())) {
			queryWrapper.eq(Blessing::getFinishTime, blessing.getFinishTime());
		}
        return blessingMapper.selectList(queryWrapper);
    }

	@Override
	public List<Blessing> selectAllOrder(BlessingDTO blessingDTO) {
		LambdaQueryWrapper<Blessing> queryWrapper = new LambdaQueryWrapper<>(Blessing.class);
		if (ObjectUtils.isNotEmpty(blessingDTO.getStatus())) {		//按照传的参数查询对应的数据
			queryWrapper.eq(Blessing::getStatus, blessingDTO.getStatus());
		}
		queryWrapper.eq(Blessing::getUserId,SecurityUtils.getUserId());
		return blessingMapper.selectList(queryWrapper);
	}

	@Override
	public Integer addBlessingOrder(BlessingDTO blessingDTO) {
		User user = userService.getById(SecurityUtils.getUserId());
		Blessing blessing = BeanUtil.copyProperties(blessingDTO, Blessing.class);
		blessing.setUserId(user.getUserId());
		blessing.setStatus(1L);
		String outTradeNum = System.currentTimeMillis() + RandomUtil.randomNumbers(6);
		blessing.setCreateTime(DateTime.now());
		blessing.setNumber(outTradeNum);
		int insert = blessingMapper.insert(blessing);
		payOrder(outTradeNum);
		return insert;
	}

	@Override
	public Integer payOrder(String outTradeNum) {
		LambdaQueryWrapper<Blessing> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(Blessing::getNumber,outTradeNum);
		Blessing blessing = this.getOne(queryWrapper);
		if(blessing == null)
			throw  new RuntimeException("订单不存在");
		if(blessing.getStatus() == 2L)
			throw  new RuntimeException("订单已支付");
		User user = userService.getById(SecurityUtils.getUserId());
		if(user.getQifuCoin().compareTo( blessing.getAmount()) <0){
			throw  new RuntimeException("余额不足");
		}
		user.setQifuCoin((user.getQifuCoin().subtract(blessing.getAmount())));
		if(userService.updateById(user)){
			blessing.setStatus(2L);
		}
		blessing.setCheckoutTime(DateTime.now());
		blessing.setFinishTime(DateTime.now());
		return blessingMapper.updateById(blessing);
	}
}
